package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Zsk;
import vip.xiaonuo.smzq.modular.kcl.entity.Zsksc;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZskResult;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZskXq;
import vip.xiaonuo.smzq.modular.kcl.mapper.WjglbMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZskMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZskscMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IZskscService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识库收藏 服务实现类
 *
 * @author sqq
 * @since 2024-10-16
 */
@Service
public class ZskscServiceImpl extends ServiceImpl<ZskscMapper, Zsksc> implements IZskscService {

    @Resource
    private ZskscMapper zskscMapper; //知识库收藏


    @Resource
    private ZskMapper zskMapper; //知识库
    @Resource
    private WjglbMapper wjglbMapper; //文件
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper; //行政区
    @Override
    public Page<Zsksc> page(Zsksc zsksc) {
        QueryWrapper<Zsksc> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 知识库收藏分页查询
     */
    @Override
    public Page<ZskResult> zskscPage(long current, long size) {
        //根据用户id获取登陆者收藏的知识库文件
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        //先查询登录这收藏过的知识库文件
        List<String> zskIds=zskscMapper.getZskIds(saBaseLoginUser.getId());

        Page<Zsk> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<ZskResult> zskResultList = new ArrayList<>();
        Page<ZskResult> returnPage = new Page<>();

        //查询分页
        LambdaQueryWrapper<Zsk> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (zskIds!=null&&zskIds.size()>0) {
            lambdaQueryWrapper.in(Zsk::getPkid, zskIds);
            page = zskMapper.selectPage(page, lambdaQueryWrapper);
            List<Zsk> zskList = page.getRecords();
            for (Zsk zskNew : zskList) {
                ZskResult zskResult = new ZskResult();
                String fileId = wjglbMapper.selectFileId(zskNew.getPkid());
                BeanUtil.copyProperties(zskNew, zskResult);
                zskResult.setFileId(fileId);

                zskResultList.add(zskResult);
            }
            returnPage.setCurrent(page.getCurrent());
            returnPage.setSize(page.getSize());
            returnPage.setRecords(zskResultList);
            returnPage.setTotal(page.getTotal());
        }
        return returnPage;
    }

    /**
     * 添加知识库收藏
     */
    @Override
    public void addzsksc(Zsksc zsksc) {
        //先将收藏过的信息删除
        LambdaQueryWrapper<Zsksc> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Zsksc::getZskid,zsksc.getZskid());
        lambdaQueryWrapper.eq(Zsksc::getUserid,zsksc.getUserid());
        Zsksc zskscOld= zskscMapper.selectOne(lambdaQueryWrapper);
        if (zskscOld!=null){
            zskscMapper.deleteById(zskscOld.getPkid());
        }
        zskscMapper.insert(zsksc);
    }


}
